﻿//using easyar;
using UnityEngine;
using DG.Tweening;

namespace BToolkit
{
    public class OffCardController : MonoBehaviour
    {
        Vector3 trackingScale;
        //ImageTargetController controller;

        void Start()
        {
            /*
            controller = transform.parent.GetComponent<ImageTargetController>();
            controller.TargetFound += OnTargetFound;
            controller.TargetLost += OnTargetLost;
            */
        }

        void OnTargetFound()
        {
            AllTargetAsCameraChildrenSwapToTracking();
        }

        void OnTargetLost()
        {
            SwapToScreen();
        }

        public static void AllTargetAsCameraChildrenSwapToTracking()
        {
            OffCardController[] controllers = Camera.main.GetComponentsInChildren<OffCardController>();
            for (int i = 0; i < controllers.Length; i++)
            {
                controllers[i].SwapToTracking();
            }
        }

        void SwapToScreen()
        {
            if (transform.localScale != Vector3.zero && transform.parent != Camera.main.transform)
            {
                if (trackingScale == Vector3.zero)
                {
                    trackingScale = transform.localScale;
                }
                transform.SetParent(Camera.main.transform, true);
                transform.localEulerAngles = Vector3.zero;
                float tweenDuration = 0.3f;
                Vector3 toPos = new Vector3(0, 0, Camera.main.nearClipPlane + 0.0001f);
                transform.DOLocalMove(toPos, tweenDuration).SetEase(Ease.OutExpo);
                Vector2 cameraViewSize = GetCameraViewSize(toPos.z);
                Vector3 scale = Vector3.one;
                if (trackingScale.x / trackingScale.y > cameraViewSize.x / cameraViewSize.y)
                {
                    scale.x = cameraViewSize.x;
                    scale.y = scale.x * trackingScale.y / trackingScale.x;
                }
                else
                {
                    scale.y = cameraViewSize.y;
                    scale.x = scale.y * trackingScale.x / trackingScale.y;
                }
                transform.DOScale(scale, tweenDuration).SetEase(Ease.OutExpo);

                CloudRecognizer.instance.btnCloseARVideo.gameObject.SetActive(true);
            }
        }

        public void SwapToTracking()
        {
            CloudRecognizer.instance.btnCloseARVideo.gameObject.SetActive(false);
            /*
            if (trackingScale != Vector3.zero && transform.parent != controller.transform)
            {
                Tween.StopMove(transform);
                Tween.StopScale(transform);
                transform.SetParent(controller.transform, true);
                transform.localEulerAngles = Vector3.zero;
                transform.localPosition = Vector3.zero;
                transform.localScale = trackingScale;
            }
            */
        }

        Vector2 GetCameraViewSize(float distance)
        {
            //EasyAR使用的fieldOfView是固定是48度
            float halfFOV = 48f * 0.5f * Mathf.Deg2Rad;
            float height = distance * Mathf.Tan(halfFOV) * 2f;
            float width = height * Camera.main.aspect;
            return new Vector2(width, height);
        }
    }
}